package com.example.shiwuyouproject.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmFragment
import com.example.shiwuyouproject.databinding.FragmentHomeBinding
import com.example.shiwuyouproject.ui.ApplyForPartTimeCoursesActivity
import com.example.shiwuyouproject.ui.WebViewActivity
import com.example.shiwuyouproject.ui.fragment.home.activity.CourseDetailsActivity
import com.example.shiwuyouproject.ui.fragment.home.activity.LatestRecruitmentActivity
import com.example.shiwuyouproject.ui.fragment.home.adapter.homeAdapter
import com.example.shiwuyouproject.ui.fragment.home.bean.GetHomeDashboardBean
import com.example.shiwuyouproject.ui.fragment.home.bean.HomeRecommendListDataBean
import com.example.shiwuyouproject.ui.modelview.HomeViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.utils.ActStartUtils
import com.example.shiwuyouproject.utils.GlideUtils
import com.example.shiwuyouproject.view.LocalImageLoader
import com.makeramen.roundedimageview.RoundedImageView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.Banner

/**
 * 首页
 */
class HomeFragment : BaseVmFragment<FragmentHomeBinding, HomeViewModel>() {
    override fun viewModelClass(): Class<HomeViewModel> = HomeViewModel::class.java
    override fun getViewBinding(): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(
            layoutInflater
        )
    }

    private var headerView: View? = null
    private var MyBanner: Banner? = null
    private var rdJingXuanImg1: RoundedImageView? = null
    private var tvJingXuan1: TextView? = null
    private var rdJingXuanImg2: RoundedImageView? = null
    private var tvJingXuan2: TextView? = null
    private var tvLianXi: TextView? = null
    private var tvTuiJian: TextView? = null
    private var tvKeJian: TextView? = null
    private var tvKaoShiBiBei: TextView? = null
    override fun initView() {
        headerView = LayoutInflater.from(context).inflate(R.layout.view_home_header, null)
        MyBanner = headerView?.findViewById(R.id.myBanner)
        rdJingXuanImg1 = headerView?.findViewById(R.id.rdJingXuanImg1)
        tvJingXuan1 = headerView?.findViewById(R.id.tvJingXuan1)
        rdJingXuanImg2 = headerView?.findViewById(R.id.rdJingXuanImg2)
        tvJingXuan2 = headerView?.findViewById(R.id.tvJingXuan2)
        tvLianXi = headerView?.findViewById(R.id.tvLianXi)
        tvTuiJian = headerView?.findViewById(R.id.tvTuiJian)
        tvKeJian = headerView?.findViewById(R.id.tvKeJian)
        tvKaoShiBiBei = headerView?.findViewById(R.id.tvKaoShiBiBei)
    }

    private var adapter: homeAdapter? = null
    private var homePage = 1
    override fun initData() {
        adapter = homeAdapter()
        binding?.homeRecyclerView?.adapter = adapter
        headerView?.let { adapter?.addHeaderView(it) }
        binding?.refreshLayout?.autoRefresh()
        binding?.refreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                homePage = 1
                mViewModel.getHomeDashboard()
                mViewModel.getCommissionLists(homePage.toString())
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                homePage++
                mViewModel.getCommissionLists(homePage.toString())
            }
        })
    }

    override fun setListener() {
        headerView?.findViewById<RelativeLayout>(R.id.xin_xi)?.setOnClickListener { //完善个人资料
            ActStartUtils.startAct(context, ApplyForPartTimeCoursesActivity::class.java)
        }
        rdJingXuanImg1?.setOnClickListener {
            if (getHome != null) {
                if (getHome?.hot?.size == 0) {
                    return@setOnClickListener
                }
                val mBundle = Bundle()
                mBundle.putString("url", getHome?.hot?.get(0)?.link)
                ActStartUtils.startAct(context, WebViewActivity::class.java, mBundle)
            }
        }
        rdJingXuanImg2?.setOnClickListener {
            if (getHome != null) {
                if (getHome?.hot?.size!! < 2) {
                    return@setOnClickListener
                }
                val mBundle = Bundle()
                mBundle.putString("url", getHome?.hot?.get(1)?.link)
                ActStartUtils.startAct(context, WebViewActivity::class.java, mBundle)
            }
        }
        tvLianXi?.setOnClickListener {//练习
            ActStartUtils.startAct(context, LatestRecruitmentActivity::class.java)
        }
        tvTuiJian?.setOnClickListener {//推荐有奖
            if (getHome != null) {
                val mBundle = Bundle()
                mBundle.putString("url", getHome?.recommend_url)
                ActStartUtils.startAct(context, WebViewActivity::class.java, mBundle)
            }
        }
        tvKeJian?.setOnClickListener {//课件
            if (getHome != null) {
                val mBundle = Bundle()
                mBundle.putString("url", getHome?.courseware_url)
                ActStartUtils.startAct(context, WebViewActivity::class.java, mBundle)
            }
        }
        tvKaoShiBiBei?.setOnClickListener {//考试必备

        }
        MyBanner?.setOnBannerListener {
            if (getHome == null) {
                return@setOnBannerListener
            }
            if (getHome?.ads?.size == 0) {
                return@setOnBannerListener
            }
            val mBundle = Bundle()
            if (getHome?.ads?.get(it)?.type == "1") {//type 1是url   2是招聘详情
                mBundle.putString("url",getHome?.ads?.get(it)?.arg)
                ActStartUtils.startAct(context, WebViewActivity::class.java, mBundle)
            }else{
                mBundle.putString("post_id",getHome?.ads?.get(it)?.arg)
                ActStartUtils.startAct(context, CourseDetailsActivity::class.java, mBundle)
            }
        }
        adapter?.setOnItemClickListener { adapter, view, position ->
            val bean:HomeRecommendListDataBean= adapter?.getItem(position) as HomeRecommendListDataBean
            val mBundle = Bundle()
            mBundle.putString("post_id", bean.id)
            ActStartUtils.startAct(context, CourseDetailsActivity::class.java, mBundle)
        }
    }

    private var getHome: GetHomeDashboardBean? = null
    override fun observe() {
        mViewModel.getHomeDashboardStatus.observe(this, androidx.lifecycle.Observer<GetHomeDashboardBean>{
            getHome = it
            val myImages: MutableList<String> = ArrayList()
            it.ads.forEach {
                myImages.add(it.image)
            }
            MyBanner?.setImages(myImages)
            MyBanner?.setImageLoader(LocalImageLoader())
            MyBanner?.start()
            if (it.hot.isNotEmpty()) {
                context?.let { it1 ->
                    rdJingXuanImg1?.let { it2 ->
                        GlideUtils.fangImgPortrait(
                            it1,
                            it2, it.hot[0].image
                        )
                    }
                }
                tvJingXuan1?.text = it.hot[0].title
            }
            if (it.hot.size > 1) {
                context?.let { it1 ->
                    rdJingXuanImg2?.let { it2 ->
                        GlideUtils.fangImgPortrait(
                            it1,
                            it2, it.hot[1].image
                        )
                    }
                }
                tvJingXuan2?.text = it.hot[1].title
            }
        })
        mViewModel.getHomeRecommendList.observe(this, androidx.lifecycle.Observer<MutableList<HomeRecommendListDataBean>>{
            if (homePage == 1) {
                binding?.refreshLayout?.finishRefresh()
                adapter?.setNewData(it as List<HomeRecommendListDataBean?>?)
            } else {
                binding?.refreshLayout?.finishLoadMore()
                adapter?.addData(it)
            }
            if (it.size == 0) {
                binding?.refreshLayout?.finishLoadMoreWithNoMoreData()
            }
        })
    }
}
